//
//  TYZLoadingView.m
//  TYZ_SC
//
//  Created by 李贤立 on 2017/12/20.
//  Copyright © 2017年 Capfer. All rights reserved.
//

#import "TYZLoadingView.h"
#import "TYZLoadingImageView.h"
#import "MBProgressHUD.h"
/// 正在加载中....
#define TYZLoading                 @""
#define TYZLoadingViewTag          TAG_LIBRARY_LoadingView
#define TYZLoadingImageViewHeight  130
@implementation TYZLoadingView
/**
 *  显示成功信息
 */
+ (void)showSuccess:(NSString *)success
{
    [self showSuccess:success complement:nil];
}
/**
 *  显示成功信息
 */
+ (void)showSuccess:(NSString *)success complement:(void(^)(void))complement {
    [self showSuccess:success toView:nil complement:complement];
}
/**
 *  显示成功信息
 *
 *  @param success 信息内容
 *  @param view    显示信息的视图
 */
+ (void)showSuccess:(NSString *)success toView:(UIView *)view {
    [self showSuccess:success toView:view complement:nil];
}
/**
 *  显示成功信息
 *
 *  @param success 信息内容
 *  @param view    显示信息的视图
 */
+ (void)showSuccess:(NSString *)success toView:(UIView *)view complement:(void(^)(void))complement {
    [self show:success icon:@"success.png" view:view complement:complement];
}

/**
 *  显示错误信息
 */
+ (void)showError:(NSString *)error {
    [self showError:error complement:nil];
}
/**
 *  显示错误信息
 */
+ (void)showError:(NSString *)error complement:(void(^)(void))complement {
    [self showError:error toView:nil complement:complement];
}
/**
 *  显示错误信息
 *
 *  @param error 错误信息内容
 *  @param view  需要显示信息的视图
 */
+ (void)showError:(NSString *)error toView:(UIView *)view {
    [self showError:error toView:view complement:nil];
}
/**
 *  显示错误信息
 *
 *  @param error 错误信息内容
 *  @param view  需要显示信息的视图
 */
+ (void)showError:(NSString *)error toView:(UIView *)view complement:(void(^)(void))complement {
    [self show:error icon:@"error.png" view:view complement:complement];
}

/**
 *  显示一般信息
 *
 */
+ (void)showNormal:(NSString *)normal {
    [self showNormal:normal complement:nil];
}
/**
 *  显示一般信息
 *
 */
+ (void)showNormal:(NSString *)normal complement:(void(^)(void))complement {
    [self showNormal:normal toView:nil complement:complement];
}
/**
 *  显示一般信息
 *
 *  @param normal 错误信息内容
 *  @param view  需要显示信息的视图
 */
+ (void)showNormal:(NSString *)normal toView:(UIView *)view {
    [self showNormal:normal toView:view complement:nil];
}
/**
 *  显示一般信息
 *
 *  @param normal 错误信息内容
 *  @param view  需要显示信息的视图
 */
+ (void)showNormal:(NSString *)normal toView:(UIView *)view complement:(void(^)(void))complement {
    [self show:normal icon:nil view:view complement:complement];
}

/**
 *  显示信息
 *
 *  @param text 信息内容
 *  @param icon 图标
 *  @param view 显示的视图
 */
+ (void)show:(NSString *)text icon:(NSString *)icon view:(UIView *)view complement:(void(^)(void))complement {
    [self hideHUD];
    if (view == nil) view = [TYZSystemDialog getCurrentViewController].view;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.minShowTime = Value_VisionStopOverTime;
    hud.label.text = text;
    hud.label.adjustsFontSizeToFitWidth = YES;
    // 设置文字颜色
    hud.contentColor = [UIColor whiteColor];
    // 设置背景颜色
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [UIColor blackColor];
    // 设置图片
    if (icon && icon.length > 0) {
        hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:[NSString stringWithFormat:@"MBProgressHUD.bundle/%@", icon]]];
        // 再设置模式
        hud.mode = MBProgressHUDModeCustomView;
    } else {
        // 再设置模式
        hud.mode = MBProgressHUDModeText;
    }
    // 隐藏时候从父控件中移除
    hud.removeFromSuperViewOnHide = YES;
    // 根据文字长短获取延迟时间
    CGFloat delay = text.length * 0.1 + 0.5;
    // 之后再消失
    //    [hud hide:YES afterDelay:delay];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [hud hideAnimated:YES];
        if (complement) {
            complement();
        }
    });
}

/**
 *  显示持续信息，默认显示正在加载中...
 *
 *  @return 直接返回一个MBProgressHUD，需要手动关闭
 */
+ (MBProgressHUD *)showMessage {
    return [self showMessage:TYZLoading];
}
/**
 *  显示持续信息
 *
 *  @param message 信息内容
 *
 *  @return 直接返回一个MBProgressHUD，需要手动关闭
 */
+ (MBProgressHUD *)showMessage:(NSString *)message {
    return [self showMessage:message toView:nil];
}

/**
 *  显示持续信息
 *
 *  @param message 信息内容
 *  @param view    需要显示信息的视图
 *
 *  @return 直接返回一个MBProgressHUD，需要手动关闭
 */
+ (MBProgressHUD *)showMessage:(NSString *)message toView:(UIView *)view {
    [self hideHUD];
    if (view == nil) view = [TYZSystemDialog getCurrentViewController].view;
    // 快速显示一个提示信息
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:view animated:YES];
    hud.minShowTime = Value_VisionStopOverTime;
    hud.label.text = message;
    hud.label.adjustsFontSizeToFitWidth = YES;
    // 设置文字颜色
    hud.contentColor = [UIColor whiteColor];
    // 设置背景颜色
    hud.bezelView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.bezelView.backgroundColor = [UIColor blackColor];
    // 再设置模式
//    hud.mode = MBProgressHUDModeCustomView;
    // 设置图片
//    UIImageView *imageView = [[UIImageView alloc] init];
//    hud.customView = imageView;
//    imageView.animationImages = BaseInfo.imageArray;
//    imageView.animationDuration = 1.0;
//    imageView.animationRepeatCount = 0;
//    [imageView startAnimating];
//    [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerX.equalTo(hud.customView);
//    }];
    // YES代表需要蒙版效果
    hud.removeFromSuperViewOnHide = YES;
    // 隐藏时候从父控件中移除
    hud.backgroundView.style = MBProgressHUDBackgroundStyleSolidColor;
    hud.backgroundView.color = [UIColor colorWithWhite:0.f alpha:.2f];
    return hud;
}

/**
 *  手动关闭MBProgressHUD
 */
+ (void)hideHUD {
    [self hideHUDForView:nil];
}

/**
 *  手动关闭MBProgressHUD
 *
 *  @param view    显示MBProgressHUD的视图
 */
+ (void)hideHUDForView:(UIView *)view {
    if (view == nil) view = [TYZSystemDialog getCurrentViewController].view;
    [MBProgressHUD hideHUDForView:view animated:YES];
}
@end
